<template>
  <div class="box">
    <div class="login">
      <div class="left">
        <img src="../../../src/assets/images/login/register.png" alt="">
      </div>
      <div class="right">
        <el-form label-position="top" label-width="100px" :model="resetData" style="max-width: 460px">
          <h3>重置密码</h3>
          <el-form-item label="邮箱">
            <el-input v-model.trim="resetData.email" />
          </el-form-item>
          <el-button @click="onReset" type="info">发送</el-button>
          <div class="bottemSpan">
            <span>您已有账号？<el-link @click="onLogin" type="primary" :underline="false">用户登录</el-link></span>
            <span>建立新账号？<el-link @click="onRegister" type="primary" :underline="false">用户注册</el-link></span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'
// import axios from 'axios'
export default {
  setup() {
    const { proxy } = getCurrentInstance()
    const router = useRouter()
    const store = useStore()
    const resetData = reactive({
      email: ''
    })
    // 跳转到登录页面
    const onLogin = () => {
      router.push({
        name: 'login'
      })
    }
    // 跳转到注册页面
    const onRegister = () => {
      router.push({
        name: 'register'
      })
    }
    // 点击发送
    const onReset = async () => {
      await proxy.$http.post('user/reset', {
        email: resetData.email
      }).then(({ data: res }) => {
        // console.log(res);
        if (res.code !== 200) return ElMessage({
          message: res.msg,
          type: 'error'
        })
        // 将邮箱传递给vuex
        store.commit('updataemail', resetData.email)
        console.log(store.state.email);
        // 跳转到重置密码第二界面
        router.push({
          name: 'resetPwd'
        })
      })
    }
    return {
      resetData,
      onLogin,
      onRegister,
      onReset
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  position: absolute;
  top: 25%;
  left: 28%;
  width: 45%;
  height: 60%;
  max-width: 720px;
  max-height: 512px;
  min-width: 500px;
  min-height: 300px;

  .login {
    display: flex;
    width: 100%;
    height: 100%;

    .left {
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #9A9AEB;
      width: 50%;
      height: 100%;

      img {
        width: 80%;
      }
    }

    .right {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 50%;
      height: 100%;
      background-color: #fff;
    }
  }
}

.el-button {
  width: 100%;
  margin-top: 5%;
  margin-bottom: 5%;
}

.el-form {
  margin: 20% 10%;

  h3 {
    text-align: center;
  }

  span {
    font-size: 12px;

    .el-link__inner {
      font-size: 12px;
    }
  }
}
</style>